/**
 * Author :tianhongyu
 * Date : 2018/5/3 20:17
 */
package com.tian.source.controller;

import cn.hutool.core.date.DateUtil;
import com.tian.core.beans.ResultBean;
import com.tian.core.page.Page;
import com.tian.source.model.Employee;
import com.tian.source.model.Record;
import com.tian.source.model.vo.EmployeeVo;
import com.tian.source.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private RecordService recordService;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private JobService jobService;

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 添加员工
     * @param employee
     * @param record
     * @return
     */
    @PostMapping("/addEmployee")
    public ResultBean<String> add(Employee employee,Record record){

        record.setCreateTime(DateUtil.date());
        recordService.add(record);
        employee.setRecordId(record.getRecordId());
        //新添加默认--试用
        employee.setStatus(1);
        employee.setCreateTime(DateUtil.date());
        employeeService.add(employee);
        return new ResultBean<>("添加成功");
    }

    /**
     * 分页查询数据
     * @param pageNum
     * @param pageSize
     * @param deptId
     * @param jobId
     * @param name
     * @return
     */
    @GetMapping("/getAllByPage")
    public ResultBean<Page<List<EmployeeVo>>> getAllByPage(Integer pageNum,Integer pageSize,Integer deptId,Integer jobId,String name){
        Page page = new Page(pageNum,pageSize);
        return new ResultBean<>(employeeService.getAllRecordsByPage(page,deptId,jobId,name));
    }

    @GetMapping("/getInfoById")
    public ResultBean<Map<String,Object>> getInfoById(Integer employeeId){
        Map<String,Object> map = new HashMap<>();
        map.put("employee",employeeService.findInfoById(employeeId));
        map.put("dept",departmentService.getAll());
        map.put("job",jobService.getAll());
        return new ResultBean<>(map);
    }

    @PostMapping("/updateRecord")
    public ResultBean<String> updateRecord(Employee employee,Record record){
        employeeService.modifyById(employee);
        recordService.modifyById(record);
        return new ResultBean<>("修改成功");
    }

    @GetMapping("/getStatusById")
    public ResultBean<Map<String,Object>> getStatusById(Integer id){
        Map<String ,Object> map = new HashMap<>();
        map.put("employee",employeeService.findStatusById(id));
        map.put("status",dictionaryService.findEmployeeStatus());
        return new ResultBean<>(map);
    }

    @PostMapping("/updateStatus")
    public ResultBean updateStatus(Employee employee){
        employeeService.modifyById(employee);
        return new ResultBean();
    }

    @GetMapping("/getEmployeeNumber")
    public ResultBean<Map<String,Object>> getEmployeeNumber(Integer deptId){
        Map<String,Object> map = new HashMap<>();
        List<Integer> jobId = jobService.findJobIdByDeptId(deptId);
        if (jobId!=null){
            for (Integer id:jobId){
                map.put(jobService.findNameById(id),employeeService.findEmployeeNum(id));
            }
        }
        return new ResultBean(map);
    }
}
